<?php

namespace backend\controllers;

use Yii;
use backend\models\Forum;
use backend\models\ForumSearch;
use backend\base\BaseBackendController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;

use base\YiiMobo;
/**
 * ForumController implements the CRUD actions for Forum model.
 */
class ForumController extends BaseBackendController
{
    public function behaviors()
    {
        return [
            'access' => [
				//动作过滤器
		        //http://www.yiichina.com/doc/api/2.0/yii-filters-accesscontrol
                'class' => AccessControl::className(),
                //'only' => ['logout', 'signup'],
                'rules' => [
                    [
                        'actions' => ['signup'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
					    //登录状态下允许的action
                        'actions' => ['index', 'update', 'create'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
					[
                        'actions' => ['index'],
                        'allow' => true,
                        'matchCallback' => function ($rule, $action) {
                            //自定义的验证方法
							return date('d-m') === '31-10';
                        }
                    ],
                ],
            ],
			//VerbFilter操作过滤器,过滤器通过HTTP请求方法。
			//http://www.yiichina.com/doc/api/2.0/yii-filters-verbfilter
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],//POST请求
                ],
            ],
        ];
    }

    //独立的动作，运用的场景较少
    public function actions()
    {
        return [
            'about' => [
                'class' => 'backend\actions\common\Page',
                'view' => 'about',
            ],
        ];
    }
	
    /**
     * Lists all Forum models.
     * @return mixed
     */
    public function actionIndex()
    {
		//exit($this->getUniqueId());
        $param['sql'] = 'select * from {{%forum}}';
        $param['order'] = 'order by forumid desc';
		//搜索的一个参数传递
        $param['param']['search'] = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';
		$param['pageparam']['PageSize'] = 2;
		echo $this->pageView('index', $param);
    }

    /**
     * Displays a single Forum model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Forum model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        //$this->CreateUpdate();
		$model = self::findModel();
        echo $this->formSave($model);
    }

    /**
     * Updates an existing Forum model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($forumid)
    {
		//self::CreateUpdate($forumid);
		$model = self::findModel($forumid);
        echo $this->formSave($model, [], function(){
			//保存数据后回调，这里可以写比如联表的一些关联操作。
			//默认保存完后跳转到index
		});
    }
    
    
	/**
	 * 添加修改数据内容.
     * 添加数据库内容的表单提交处理
	 * 模板文件通常为  _form.php
	 */
	public function CreateUpdate($forumid = null)
	{
		
		if ($model->load(Yii::$app->request->post()))
		{
			if($model->save())
            {
				if (Yii::$app->request->getIsAjax()){
					$this->echojson();
				}else{
			        $this->OperTips('/forum/index');
				}
			}
		}
     	echo $this->render('_form',[
			'model'=>$model,
		]);
	}
    /**
	 * 批量操作（删除）一些记录
	 * 一般来自控制器的默认方法index
	 * @param $id 一般为数据库表的主键值
	 */
	public function actionBatch()
	{
		$model = self::findModel();
		$this->formBatch($model, function(){
			echo 111111111111111111;
			
		});
	}
    /**
     * Deletes an existing Forum model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        $this->OperTips();
    }

    /**
     * Finds the Forum model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Forum the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id = null)
    {
        $model = $id ? backend\models\Forum::findOne($id) : new Forum;
        if ($model !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
